﻿using System;
using System.Collections.Generic;
using System.Data;
using DevelopTool.Bll.DbBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using DevelopTool.Model.DbModels;

namespace DevelopTool.Forms.DbForms.JobForms
{
    public partial class OracleJobForm : BaseUIPage
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;

        /// <summary>
        /// Oracle操作类
        /// </summary>
        private OracleDbBll oracleDbBll;

        /// <summary>
        /// 查询条件
        /// </summary>
        private string queryKeyword;

        /// <summary>
        /// 查询实例
        /// </summary>
        private string queryDataBase;

        /// <summary>
        /// 查询资源名
        /// </summary>
        private string queryInfoName;

        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("JOB"){ColumnText="标识符",Width=-1,ColumnType = ColumnType.DefaultFilter|ColumnType.RowMain}},
                { new DataGridColumn("LOG_USER"){ColumnText="提交作业用户",Width=-1}},
                { new DataGridColumn("PRIV_USER"){ColumnText="应用作业用户",Width=-1}},
                { new DataGridColumn("SCHEMA_USER"){ColumnText="分析作业用户",Width=-1}},
                { new DataGridColumn("BROKEN"){ColumnText="状态",Width=-1}},
                { new DataGridColumn("LAST_DATE"){ColumnText="上次成功执行日期",Width=-1}},
                { new DataGridColumn("LAST_SEC"){ColumnText="上次成功执行时间",Width=-1}},
                { new DataGridColumn("THIS_DATE"){ColumnText="开始执行日期",Width=-1}},
                { new DataGridColumn("THIS_SEC"){ColumnText="开始执行时间",Width=-1}},
                { new DataGridColumn("NEXT_DATE"){ColumnText="下次执行日期",Width=-1}},
                { new DataGridColumn("NEXT_SEC"){ColumnText="下次执行时间",Width=-1}},
                { new DataGridColumn("TOTAL_TIME"){ColumnText="总共执行时间",Width=-1}},                
                { new DataGridColumn("INTERVAL"){ColumnText="间隔",Width=0}},
                { new DataGridColumn("FAILURES"){ColumnText="失败次数",Width=-1}},
                { new DataGridColumn("WHAT"){ColumnText="正文",Width=0,ColumnType = ColumnType.DefaultFilter|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="JOB"
                } }},
                { new DataGridColumn("NLS_ENV"){ColumnText="会话参数",Width=0,ColumnType = ColumnType.DefaultFilter | ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="JOB"
                } }},
                { new DataGridColumn("MISC_ENV"){ColumnText="其他会话参数",Width=0}},
              };


        public OracleJobForm(DbInfo dbInfo)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
        }

        public OracleJobForm(DbInfo dbInfo,string queryKeyword)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
            this.queryKeyword = queryKeyword;
        }

        public OracleJobForm(DbInfo dbInfo,string queryDataBase,string queryInfoName)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
            this.queryDataBase = queryDataBase;
            this.queryInfoName = queryInfoName;
        }

        private void OracleJobForm_Load(object sender,EventArgs e)
        {
            this.oracleDbBll = new OracleDbBll(dbInfo);

            InitDate();

            this.dataDisplayControl.DbInfo = this.dbInfo;
        }


        private void InitDate()
        {
            DataTable dataTable = new DataTable();
            LoadingHelper.ShowLoading("获取job数据",() =>
            {
                dataTable = oracleDbBll.GetJobs(this.queryDataBase);
            });

            this.dataDisplayControl.BindData(dataTable,this.dataGridColumns,"JOB ASC");

            if(!string.IsNullOrEmpty(queryKeyword)) this.dataDisplayControl.Search(queryKeyword);
        }


        private void dataDisplayControl_DataReLoad(object sender,EventArgs e)
        {
            InitDate();
        }
    }
}
